using ClassDemo.Model.Entity;
using ClassDemo.Repository;


namespace ClassDemo.Service;

/// <summary>
/// 教师服务实现
/// </summary>
public class TeacherService : ITeacherService
{
    private readonly ITeacherRepository _teacherRepository;

    public TeacherService(ITeacherRepository teacherRepository)
    {
        _teacherRepository = teacherRepository;
    }

    /// <summary>
    /// 获取所有教师
    /// </summary>
    /// <returns>教师列表</returns>
    public async Task<List<Teacher>> GetAllTeachersAsync()
    {
        return await _teacherRepository.GetAllAsync();
    }

    /// <summary>
    /// 根据ID获取教师
    /// </summary>
    /// <param name="id">教师ID</param>
    /// <returns>教师信息</returns>
    public async Task<Teacher?> GetTeacherByIdAsync(int id)
    {
        return await _teacherRepository.GetByIdAsync(id);
    }

    /// <summary>
    /// 根据手机号获取教师
    /// </summary>
    /// <param name="mobile">手机号</param>
    /// <returns>教师信息</returns>
    public async Task<Teacher?> GetTeacherByMobileAsync(string mobile)
    {
        return await _teacherRepository.GetByMobileAsync(mobile);
    }

    /// <summary>
    /// 分页获取教师列表
    /// </summary>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">页大小</param>
    /// <param name="keyword">搜索关键词</param>
    /// <returns>分页结果</returns>
    public async Task<(List<Teacher> Data, int Total)> GetTeachersPageAsync(int pageIndex, int pageSize, string? keyword = null)
    {
        if (string.IsNullOrWhiteSpace(keyword))
        {
            return await _teacherRepository.GetPageListAsync(pageIndex, pageSize);
        }
        
        return await _teacherRepository.GetPageListAsync(pageIndex, pageSize, 
            t => t.Name.Contains(keyword) || t.Mobile.Contains(keyword));
    }

    /// <summary>
    /// 根据姓名搜索教师
    /// </summary>
    /// <param name="name">教师姓名</param>
    /// <returns>教师列表</returns>
    public async Task<List<Teacher>> SearchTeachersByNameAsync(string name)
    {
        return await _teacherRepository.GetByNameAsync(name);
    }

    /// <summary>
    /// 添加教师
    /// </summary>
    /// <param name="teacher">教师信息</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> AddTeacherAsync(Teacher teacher)
    {
        // 数据验证
        if (string.IsNullOrWhiteSpace(teacher.Name))
        {
            return (false, "姓名不能为空");
        }
        
        if (string.IsNullOrWhiteSpace(teacher.Mobile))
        {
            return (false, "手机号不能为空");
        }

        // 检查手机号是否已存在
        if (await _teacherRepository.IsMobileExistsAsync(teacher.Mobile))
        {
            return (false, "手机号已存在");
        }

        try
        {
            var result = await _teacherRepository.AddAsync(teacher);
            return result ? (true, "添加成功") : (false, "添加失败");
        }
        catch (Exception ex)
        {
            return (false, $"添加失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 更新教师信息
    /// </summary>
    /// <param name="teacher">教师信息</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> UpdateTeacherAsync(Teacher teacher)
    {
        // 数据验证
        if (teacher.ID <= 0)
        {
            return (false, "教师ID无效");
        }
        
        if (string.IsNullOrWhiteSpace(teacher.Name))
        {
            return (false, "姓名不能为空");
        }
        
        if (string.IsNullOrWhiteSpace(teacher.Mobile))
        {
            return (false, "手机号不能为空");
        }

        // 检查教师是否存在
        var existingTeacher = await _teacherRepository.GetByIdAsync(teacher.ID);
        if (existingTeacher == null)
        {
            return (false, "教师不存在");
        }

        // 检查手机号是否被其他教师使用
        if (await _teacherRepository.IsMobileExistsAsync(teacher.Mobile, teacher.ID))
        {
            return (false, "手机号已被其他教师使用");
        }

        try
        {
            var result = await _teacherRepository.UpdateAsync(teacher);
            return result ? (true, "更新成功") : (false, "更新失败");
        }
        catch (Exception ex)
        {
            return (false, $"更新失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 删除教师
    /// </summary>
    /// <param name="id">教师ID</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> DeleteTeacherAsync(int id)
    {
        if (id <= 0)
        {
            return (false, "教师ID无效");
        }

        // 检查教师是否存在
        var teacher = await _teacherRepository.GetByIdAsync(id);
        if (teacher == null)
        {
            return (false, "教师不存在");
        }

        try
        {
            var result = await _teacherRepository.DeleteByIdAsync(id);
            return result ? (true, "删除成功") : (false, "删除失败");
        }
        catch (Exception ex)
        {
            return (false, $"删除失败：{ex.Message}");
        }
    }

    /// <summary>
    /// 批量删除教师
    /// </summary>
    /// <param name="ids">教师ID列表</param>
    /// <returns>操作结果</returns>
    public async Task<(bool Success, string Message)> DeleteTeachersAsync(List<int> ids)
    {
        if (ids == null || ids.Count == 0)
        {
            return (false, "请选择要删除的教师");
        }

        try
        {
            var result = await _teacherRepository.DeleteAsync(t => ids.Contains(t.ID));
            return result > 0 ? (true, $"成功删除{result}个教师") : (false, "删除失败");
        }
        catch (Exception ex)
        {
            return (false, $"删除失败：{ex.Message}");
        }
    }
}